#pragma once

#include "LinkedList.h"
#include "Singleton.h"

class CServer;

struct ServerPtr
{
    CServer *pSvr;

    ServerPtr() { pSvr = nullptr; }
    virtual ~ServerPtr() { DestroyServer(); }

    bool NewServer();
    void DestroyServer();
};

typedef CLinkedList<ServerPtr, CSpinLock> CServerList;
typedef CServerList::Node ServerNode;

class CSvrMgr : public CSingleton<CSvrMgr>
{
    friend class CSingleton;

public:
    ~CSvrMgr();
    void operator delete(void *ptr);

public:
    int CreateListenSocket();
    void CloseListenSocket();

    ServerNode *CreateServerNode();
    void DestroyServerNode(ServerNode *pNode);

    int GetListenSocket() { return m_ilfd; }
    void Destroy();

    CServer *GetAcceptServer();
    void StopServer();

private:
    CSvrMgr();
    void *operator new(size_t sz);

private:
    int m_ilfd;           // 监听端口
    CServerList m_lstSvr; // 服务进程列表
};
